﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Hypermarket_Serial_Port
{
    class RequestLastItemInfoManager : CashierRegisterTaskManager
    {
        #region Constructor
        public RequestLastItemInfoManager()
        {
        }
        #endregion

        #region Operation for Request Lst Item Info
        public override List<string> TaskOperation(List<string> receivedInfoList, Dictionary<string, CashierRegister> cashierDict)
        {
            try
            {
                SerialPortLog.Log("Request Lst Item Info Start");
                CashierRegister cashierRegister;
                string machineID = receivedInfoList[Constant.ZERO];
                string type = receivedInfoList[Constant.ONE];
                SerialPortLog.LogOperation("Request Lst Item Info: " + machineID);

                if (cashierDict.TryGetValue(machineID, out cashierRegister))
                {
                    return RequestLastInfo(cashierRegister, machineID, type);
                }
                else
                {
                    //return Constant.LOGIN_FIRST_MSG + Constant.NEWLINE;
                    List<string> output = new List<string>();
                    output.Add(machineID);
                    output.Add(type);
                    output.Add(Constant.FAIL);
                    SerialPortLog.LogOperation("Request Lst Item Info Fail");
                    SerialPortLog.Log("Request Lst Item Info Fail");
                    return output;
                }
            }
            catch
            {
                //return Constant.PROGRAM_ERROR + Constant.NEWLINE;
                SerialPortLog.LogOperation("Request Lst Item Info Error");
                SerialPortLog.Log("Request Lst Item Info Error");
                throw;
            }
        }
        #endregion

        private List<string> RequestLastInfo(CashierRegister cashierRegister, string machineID, string type)
        {
            if (cashierRegister.isTransactionStarted())
            {
                string lastBarcode = cashierRegister.getRequestLastItem();
                DataRow[] rows = GetNoDiscountProductDataRows(lastBarcode);
                if (rows.Count() != Constant.ZERO)
                {
                    return WithoutDiscLastItemInfo(lastBarcode, cashierRegister, machineID, rows.First(), type);
                }
                else
                {
                    List<string> tempStr = new List<string>();
                    tempStr.Add(machineID);
                    tempStr.Add(type);
                    tempStr.Add(Constant.FAIL);
                    SerialPortLog.LogOperation("Request Lst Item Info Fail");
                    SerialPortLog.Log("Request Lst Item Info Fail");
                    return tempStr;
                }
            }
            else
            {
                List<string> tempStr = new List<string>();
                tempStr.Add(machineID);
                tempStr.Add(type);
                tempStr.Add(Constant.FAIL);
                SerialPortLog.LogOperation("Request Lst Item Info Fail");
                SerialPortLog.Log("Request Lst Item Info Fail");
                return tempStr;
            }

        }

        private DataRow[] GetNoDiscountProductDataRows(string productID)
        {
            DataTable Stock = mDBManager.getTable(Constant.STOCK_PRODUCT_TABLE);
            string query = Constant.PRODUCT_ID + Constant.EQUAL + Constant.SINGAL_QUOTE + productID +
                    Constant.SINGAL_QUOTE + Constant.AND + Constant.EXPIRE_DATE + Constant.MORE_THAN +
                    Constant.SINGAL_QUOTE + DateTime.Today + Constant.SINGAL_QUOTE;
            DataRow[] rows = Stock.Select(query);
            return rows;
        }

        private static List<string> WithoutDiscLastItemInfo(string productID, CashierRegister cashierRegister, string machineID, DataRow dr, string type)
        {
            string name = dr[Constant.NAME].ToString();
            string sellingPrice = dr[Constant.SELLING_PRICE].ToString();
            string batchID = dr[Constant.BATCH_ID].ToString();
            string importPrice = dr[Constant.IMPORT_PRICE].ToString();

            //cashierRegister.AddTransaction(productID, name, batchID, importPrice, sellingPrice, Constant.ZERO_PERCENTAGE, Convert.ToInt32(quantity)); //for database report purpose
            //string output = Constant.MSG_SCANNED + Constant.COLON + Constant.NEWLINE + Constant.PRODUCT_ID + Constant.COLON + Constant.SPACE + productID + Constant.NEWLINE + 
            //                Constant.NAME + Constant.COLON + Constant.SPACE + name + Constant.NEWLINE + Constant.SELLING_PRICE + Constant.COLON + Constant.SPACE + 
            //                Math.Round(Convert.ToDecimal(sellingPrice), Constant.TWO) + Constant.NEWLINE + Constant.QUANTITY + Constant.COLON + Constant.SPACE + quantity + Constant.NEWLINE  /***sound index****/;
            List<string> output = new List<string>();
            output.Add(machineID);
            output.Add(type);
            output.Add(Constant.SUCCESS);
            output.Add(name);
            output.Add(sellingPrice.ToString());
            SerialPortLog.LogOperation("Request Lst Item Info Successfully");
            SerialPortLog.Log("Request Lst Item Info Successfully");
            return output;
        }

    }
}
